<?php
namespace App\Controllers\Stat;

use App\Models\SmsWlist;

/**
 * Created by PhpStorm.
 * User: 847050412
 * Date: 2016/8/18
 * Time: 11:57
 */
class WlistController extends \App\Controllers\BaseController {

    public function initialize() {
        parent::initialize();
        if($this->request->isPost()) {
            $this->checkLogin();
        }
    }
    /**
     * 积分列表展示
     */

    /**
     * 积分管理
     */
    public function imtAction() {

        $this->display('stat/wlist/imt');
    }
    //---------------------------------------------------------------------------------
    //获取指定的白名单号码
    //---------------------------------------------------------------------------------
    public function getsAction() {
        $params = $this->dispatcher->getParams();

                $data   = [
            'page' => $this->request->getPost('page'),
            'size' => $this->request->getPost('size')
        ];

        if(is_array($params) && isset($params[0])) {
            $data = [
                'page' => $this->request->getPost('page'),
                'size' => $this->request->getPost('size'),
                //'mobile'=>$this->request->getPost('mobile')
            ];
        }
       $data['mobile'] = $this->request->getPost('mobile');
        //var_dump($data);exit;
            if($data['page'] == '' || !is_numeric($data['page'])) {
                return $this->sendAjax(false, '获取数据失败，未获取到当前的页数！');
            }
            if($data['size'] == '' || !is_numeric($data['size'])) {
               return $this->sendAjax(false, '获取数据失败，未获取到当前指定每页条数！');
           }
            $data['page'] = intval($data['page']);
            $data['size'] = intval($data['size']);
            $offset = ($data['page'] - 1) * $data['size'];
            $log    = new SmsWlist();
            $ret    = $log->gets($data['mobile'],$offset, $data['size']);
            if($ret !== false) {
                return $this->sendAjax(true, $ret);
            } else {
                return $this->sendAjax(false, "获取数据失败，系统错误！");
            }

        }




    //----------------------------------------------------------------
    //删除白名单号码
    //----------------------------------------------------------------
    public function removeAction() {
        $id = $this->request->getPost('mobile') != '' ? $this->request->getPost('mobile') : $this->request->getQuery('mobile');
        $nc = new SmsWlist();
        if($nc->remove($id)) {
            return $this->sendAjax(true, '删除成功');
        } else {
            return $this->sendAjax(false, "删除失败，系统错误！");
        }
    }

    //----------------------------------------------------------------
    // 新增白名单号码
    //----------------------------------------------------------------
    public function addAction() {
        if($this->request->isGet()) {
            $this->display('/stat/wlist/add');
        } else {
            $mobile = trim($this->request->getPost('mobile'));
            $mobilesArray = explode(',', $mobile);
            $mobiles      = [];
            foreach($mobilesArray as $v) {
                $mobiles[] = trim($v);
            }
            $nc   = new SmsWlist();
            $data = $nc->add($mobiles);
            if($data) {
                return $this->sendAjax(true, '新增白名单号码成功！');
            } else {
                return $this->sendAjax(false, '新增白名单号码失败，系统错误！');
            }
        }
    }

    //----------------------------------------------------------------
    // 多个删除白名单号码
    //----------------------------------------------------------------
    public function delsAction() {
        if($this->request->isGet()) {
            $this->display('/stat/wlist/dels');
        } else {
            $mobile       = trim($this->request->getPost('mobile'));
            $mobilesArray = explode(',', $mobile);
            $mobiles      = [];
            foreach($mobilesArray as $v) {
                $mobiles[] = trim($v);
            }
            $nc   = new SmsWlist();
            $data = $nc->dels($mobiles);
            if($data) {
                return $this->sendAjax(true, '批量删除N白名单码成功！');
            } else {
                return $this->sendAjax(false, '批量白名单号码失败，系统错误！');
            }
        }
    }

    //----------------------------------------------------------------
    // 清空并新增白名单号码
    //----------------------------------------------------------------
    public function bulkUpdateAction() {
        if($this->request->isGet()) {
            $this->display('/stat/wlist/bulkUpdate');
        } else {
            $mobile       = trim($this->request->getPost('mobile'));
            $mobilesArray = explode(',', $mobile);

            $mobiles      = [];
            foreach($mobilesArray as $v) {
                $mobiles[] = trim($v);
            }
            //var_dump($mobiles);exit;
            $wlist   = new SmsWlist();
            $data = $wlist->bulkUpdate($mobiles);
            if($data) {
                return $this->sendAjax(true, '清空并新增白名单号码成功！');
            } else {
                return $this->sendAjax(false, '清空并新增白名单号码失败，系统错误！');
            }
        }
    }


    //---------------------------------------------------------------------------------
    //获取积分得总条数
    //---------------------------------------------------------------------------------
    public function countAction() {
        $mobile = $this->request->getPost('mobile');
        $wlist = new SmsWlist();
        $ret   = $wlist->total($mobile);
        if($ret !== false) {
            return $this->sendAjax(true, $ret);
        } else {
            return $this->sendAjax(false, "获取总数失败，系统错误！");
        }
    }


}